package com.example.serverSide.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.serverSide.commom.queryData.ProfileUpdateQuery;
import com.example.serverSide.commom.queryData.SysUserAdjustQuery;
import com.example.serverSide.commom.util.BaseContext;
import com.example.serverSide.commom.util.MD5Util;
import com.example.serverSide.commom.util.TokenUtil;
import com.example.serverSide.mapper.SysDeptMapper;
import com.example.serverSide.mapper.SysUserMapper;
import com.example.serverSide.pojo.SysDept;
import com.example.serverSide.pojo.SysUser;
import com.example.serverSide.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysDeptMapper sysDeptMapper;
    @Override
    public String login(String username, String password) {
        if (!StringUtils.hasLength(username)) {
            return "Error:username is Empty";
        }
        if (!StringUtils.hasLength(password)){
            return "Error:username is Empty";
        }
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<SysUser>();
        queryWrapper.eq(SysUser::getUsername,username);

        SysUser sysUser = this.getOne(queryWrapper);
        if (sysUser == null) {
            return"Error:Username Or Password is Wrong";
        }
        boolean valid = MD5Util.validpassword(password, sysUser.getPassword());
        if (!valid) {
            return "Error:Username or Password is Invalid";
        }
        if (sysUser.getStatus()==0){
            return "Error:U've Been Fried";
        }
        LambdaQueryWrapper<SysDept> deptQueryWrapper = new LambdaQueryWrapper<>();
        deptQueryWrapper.eq(SysDept::getId,sysUser.getDeptId());
        BaseContext.setCurrentId(sysUser.getId());
        System.out.println(BaseContext.getCurrentId());
        SysDept sysDept = sysDeptMapper.selectOne(deptQueryWrapper);
        String token = TokenUtil.getJwtToken(sysUser.getId(), sysUser.getDeptId(), sysUser.getUsername(), sysUser.getImgUrl(), sysDept.getName());
        return token;
    }

    @Override
    public List<SysUser> getAllUsers() {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<SysUser>();
        List<SysUser> sysUsers = sysUserMapper.selectList(wrapper);
        sysUsers.stream().forEach(user -> {
            if (user.getStatus()==0){
                user.setStatusName("禁用(离职)");
            }else {
                user.setStatusName("正常(在职)");
            }
            Long deptId = user.getDeptId();
            List<SysDept> dept = new ArrayList<SysDept>();
            if (deptId != null){
                dept = sysDeptMapper.getDeptById(deptId);
                if (dept.size() == 0){
                    user.setDeptName("无职位");
                }else {
                    user.setDeptName(dept.get(0).getName());
                }
            }else {
                user.setDeptName("无职位");
            }
        });
        /*Page<SysUser> page = new Page<SysUser>();
        page.setCurrent(1);
        page.setSize(10);
        page = this.page(page,wrapper);*/
        return sysUsers;
    }

    @Override
    public SysUser getUserInfo(String id) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId,id);
        SysUser sysUser = this.getOne(queryWrapper);
        return sysUser;
    }

    @Override
    public String updateProfile(ProfileUpdateQuery profileUpdateQuery) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId,profileUpdateQuery.getId());
        SysUser one = this.getOne(queryWrapper);
        if(one == null){
            return "User is not exists";
        }
        SysUser sysUser = new SysUser();

        sysUserMapper.update(sysUser,queryWrapper);
        return "Profile details updated";
    }

    @Override
    public String adjustSysUser(SysUserAdjustQuery sysUserAdjustQuery) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId,sysUserAdjustQuery.getId());
        SysUser one = this.getOne(queryWrapper);
        if(one == null){
            return "User is not exists";
        }
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(sysUserAdjustQuery.getDeptId()).setStatus(sysUserAdjustQuery.getStatus());
        sysUserMapper.update(sysUser,queryWrapper);
        return "sysUserInfo updated";
    }

    @Override
    public String addNewSysUser(SysUser sysUser) {
        System.out.println(sysUser);
        String password = MD5Util.getEncryptedPwd(sysUser.getPassword());
        String realId = MD5Util.getEncryptedPwd(sysUser.getRealId());
        sysUser.setImgUrl("demo/images/sysUser/defaultAvatar.png");
        sysUser.setShowId(sysUser.getRealId().substring(sysUser.getRealId().length()-6));
        sysUser.setPassword(password);
        sysUser.setRealId(realId);
        sysUserMapper.insert(sysUser);
        return "insert success";
    }

    @Override
    public String deleteSysUser(SysUser[] sysUsers) {
        for (SysUser sysUser : sysUsers) {
            sysUserMapper.deleteById(sysUser);
        }
        return "delete success";
    }
}
